import { getOrderPageAPI } from "@/api/business/ordet";
import { OrderResult } from "@/types/order";
import { ref } from "vue";
import { useUserStore } from "@/stores/userStore";
import { getAdminOrderPageAPI } from "@/api/admin/ordet";
export default function () {
  // 订单列表
  const orders = ref<OrderResult[]>([]);
  // 搜索
  const phone = ref("");
  const number = ref("");
  const page = ref(1);
  const pageSize = ref(10);
  const total = ref(0);
  // 订单状态
  const orderStatus = ref(0);
  // 日期选择器
  const dateValue = ref<string[]>([]);
  const userStore = useUserStore()
  // tab切换
  const activeHandle = (activeStatus: number) => {
    if (activeStatus === orderStatus.value) return;
    orderStatus.value = activeStatus;
    getOrderPage();
    // 重置搜索项
    phone.value = "";
    number.value = "";
    dateValue.value = [];
  };
  // 订单分页查询
  const getOrderPage = async () => {
    let params = {
      page: page.value,
      pageSize: pageSize.value,
      businessId: userStore.rule === 'admin' ? undefined : userStore.user.id,
      number: number.value || undefined,
      phone: phone.value || undefined,
      beginTime: dateValue.value ? dateValue.value[0] : undefined,
      endTime: dateValue.value ? dateValue.value[1] : undefined,
      status: orderStatus.value === 0 ? undefined : orderStatus.value,
    };
    try {
      if (userStore.rule === 'admin') {
        let { data } = await getAdminOrderPageAPI(params)
        orders.value = data.records;
        total.value = data.total
      } else {
        let { data } = await getOrderPageAPI(params);
        orders.value = data.records;
        total.value = data.total
      }
    } catch (error) {
      console.log(error);

    }
  };

  return {
    orders,
    phone,
    number,
    page,
    pageSize,
    total,
    orderStatus,
    dateValue,
    activeHandle,
    getOrderPage,
  }
}
